﻿using AutoMapper;
using Dto;
using Microsoft.AspNetCore.Mvc;
using MyProject;
using System.Xml.Linq;
using Util;

namespace Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserInfoController : BaseController
    {
        public IFreeSql _fsql;
        private readonly IMapper _mapper;

        public UserInfoController(IFreeSql fsql,IMapper mapper)
        {
            _fsql = fsql;
            _mapper = mapper;
        }
        
       
        /// <summary>
        /// 批量查询
        /// </summary>
        [HttpPost]
        //[Authorize(Roles ="user")] 
        public ApiModel GetIds([FromBody] List<int> ids)
        {
            var t1 = _fsql.Select<UserInfoDto>().Where(a => ids.Contains(a.UserId)).ToList<UserInfoDto>();
            if (t1 == null)
            {
                return apiModel(Status.Error, "无数据");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
        //[Authorize(Roles ="admin")] 
        private ApiModel Add([FromBody] UserInfoDto data)
        {
            long id = _fsql.Insert(data).ExecuteIdentity();
            data.UserId = (int)id;
            if (id == 0)
            {
                return apiModel(Status.Error, "插入错误");
            }
            else
            {
                return apiModel(Status.Success, "", data);
            }
        }
        /// <summary>
        /// 查询他人用户信息
        /// </summary>
        [HttpPost]
        //[Authorize(Roles ="user")] 
        public ApiModel Get([FromBody] ParmUserId data)
        {
            var t1 = _fsql.Select<UserInfoDto>().Where(a => a.UserId == data.UserId).ToOne();
            if (t1 == null)
            {
                return apiModel(Status.Error, "无数据");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        public ApiModel Delete([FromBody] ParmUserId data)
        {
            var t1 = _fsql.Delete<UserInfoDto>(new { UserId = data.UserId }).ExecuteAffrows();
            //删除地址表
            _fsql.Delete<Address>().Where(a=>a.UserId == data.UserId).ExecuteAffrows();
            if (t1 == 0)
            {
                return apiModel(Status.Error, "删除错误");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
        /// <summary>
        /// 更新用户modelid,gender,nick_name,face_url,Email,ex,glb_url,local_avatar,
        /// </summary>
        [HttpPost]
        //[Authorize(Roles ="user")] 
        public ApiModel UpdateInfo([FromBody] UserInfoDto data)
        {
            var entity = _mapper.Map<UserInfo>(data);
            entity.UpdateTime = DateTime.Now;

            var t1 = _fsql.Update<UserInfo>().SetSourceIgnore(entity, col => col == null).Where(a => a.UserId == data.UserId).ExecuteAffrows();
            if (t1 == 0)
            {
                return apiModel(Status.Error, "更新错误");
            }
            else
            {
                return apiModel(Status.Success, "更新成功", t1);
            }
        }
    }
}